function formatDate(dateStr, format = 'YYYY-MM-DD hh:mm:ss') {
	let dd = new Date(dateStr)
	return format.replace('YYYY', dd.getFullYear()).replace('MM', (dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) :
			dd.getMonth() + 1)).replace('DD', dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate()).replace('hh', dd
			.getHours() <
			10 ? '0' + dd.getHours() : dd.getHours()).replace('mm', dd.getMinutes() < 10 ? '0' + dd.getMinutes() : dd
			.getMinutes())
		.replace('ss', dd.getSeconds() < 10 ? '0' + dd.getSeconds() : dd.getSeconds()).replace('ms', dd
			.getMilliseconds())
}

function timeDiff(dateStr) {
	let minute = 1000 * 60,
		hour = minute * 60,
		day = hour * 24,
		t = new Date(dateStr).getTime()
	let result = '',
		now = new Date().getTime(),
		diffValue = now - t

	if (diffValue < 0) {
		diffValue = Math.abs(diffValue)
		let dayC = diffValue / day,
			hourC = diffValue / hour,
			minC = diffValue / minute
		if (parseInt(dayC) > 30) {
			result = "" + formatDate(dateStr, "MM-DD");
		} else if (parseInt(dayC) > 1) {
			result = "" + parseInt(dayC) + "天后"
		} else if (parseInt(dayC) == 1) {
			result = "明天"
		} else if (hourC >= 1) {
			result = "" + parseInt(hourC) + "小时后"
		} else if (minC >= 1) {
			result = "" + parseInt(minC) + "分钟后"
		} else {
			result = "稍候"
		}
	} else {
		let dayC = diffValue / day,
			hourC = diffValue / hour,
			minC = diffValue / minute
		if (parseInt(dayC) > 30) {
			result = "" + formatDate(dateStr, "MM-DD");
		} else if (parseInt(dayC) > 1) {
			result = "" + parseInt(dayC) + "天前"
		} else if (parseInt(dayC) == 1) {
			result = "昨天"
		} else if (hourC >= 1) {
			result = "" + parseInt(hourC) + "小时前"
		} else if (minC >= 1) {
			result = "" + parseInt(minC) + "分钟前"
		} else {
			result = "刚刚"
		}
	}

	return result
}

// 格式化今天,明天等
function timeDiffDate(dateStr) {
	let t = new Date(dateStr),
		now = new Date()

	if (t.getFullYear() == now.getFullYear()) {
		if (t.getMonth() == now.getMonth()) {
			let diff = t.getDay() - now.getDay(),
				diffAbs = Math.abs(diff)
			if (diffAbs <= 3) {
				if (diffAbs == 0) {
					return '今天'
				} else if (diffAbs == 1) {
					return diff < 0 ? '' : '明天'
				} else if (diffAbs == 2) {
					return diff < 0 ? '' : '后天'
				}
			} else {
				// return formatDate(dateStr, "MM-DD")
			}
		} else {
			// return formatDate(dateStr, "MM-DD")
		}
	} else {
		// return formatDate(dateStr, "MM-DD")
	}
}

// 计算耗时
function countDown(startDate, endDate) {
	let startDate1 = startDate.replace(/-/g, "/")
	let endDate1 = endDate.replace(/-/g, "/")
	let date1 = new Date(startDate1), //开始时间
		date2 = new Date(endDate1),

		date3 = date2.getTime() - date1.getTime(), //时间差的毫秒数    
		//计算出相差天数
		days = Math.floor(date3 / (24 * 3600 * 1000)),
		//计算出小时数
		leave1 = date3 % (24 * 3600 * 1000), //计算天数后剩余的毫秒数
		hours = Math.floor(leave1 / (3600 * 1000)),
		//计算相差分钟数
		leave2 = leave1 % (3600 * 1000), //计算小时数后剩余的毫秒数
		minutes = Math.floor(leave2 / (60 * 1000)),
		//计算相差秒数
		leave3 = leave2 % (60 * 1000), //计算分钟数后剩余的毫秒数
		seconds = Math.round(leave3 / 1000);
	days = days < 10 ? '0' + days : days
	hours = hours < 10 ? '0' + hours : hours
	minutes = minutes < 10 ? '0' + minutes : minutes
	seconds = seconds < 10 ? '0' + seconds : seconds
	return [days, hours, minutes, seconds]
}


export default {
	// 格式化时间
	formatDate,
	// 时间格式化成几天前
	timeDiff,
	timeDiffDate,
	countDown
}
